package agentspeak.editors;

import java.util.ArrayList;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.reconciler.DirtyRegion;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.swt.widgets.Display;

/*
 * Métodos pra implementar:
 * get e set editor (TEM)
 * initialReconcile (TEM)
 * reconcile (TEM)
 * setDocument (TEM)
 * calculatePositions
 * getLinha
 * emitPosition (TEM)
 * positions
 * */

public class ASLReconcilingStrategy implements IReconcilingStrategy{
	private AgentSpeakEditor editor;
	private IDocument fDocument;
	private ArrayList fPosition = new ArrayList();
	private int fOffset;
	private int fRangeEnd;
	
    protected int cNextPos = 0;	// posição do proximo caractere
		
	@Override
	public void reconcile(IRegion region) {
		initialReconcile();
	}
	
	@Override
	public void reconcile(DirtyRegion arg0, IRegion arg1) {
		initialReconcile();
	}
	
	@Override
	public void setDocument(IDocument document) {
		this.fDocument = document;
	}
	
    public void initialReconcile() {
        fOffset = 0;
        fRangeEnd = fDocument.getLength();
        calculatePositions();

    }
	    
    protected void emitPosition(int startOffset, int length) {
        fPosition.add(new Position(startOffset, length));
    }
    
	public AgentSpeakEditor getEditor() {
		return editor;
	}
	
	public void setEditor(AgentSpeakEditor editor) {
		this.editor = editor;
	}

    protected void calculatePositions() {
        fPosition.clear();
        cNextPos = fOffset;

//        try {
//                recursiveTokens(0);
//        } catch (BadLocationException e) {
//                e.printStackTrace();
//        }

        Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                        editor.updateFoldingStructure(fPosition);
                }

        });
    }
    
    private String getLinha(int linha){
    	String output = null;
    	output = fPosition.get(linha).toString();
    	return output;
    }
    
//    public int positions(int offset){}
}
